/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.uac.dao.IActionManageDao;
import com.eelpo.cmmp.uac.dao.entity.TAction;
import com.eelpo.cmmp.uac.dao.vo.Action;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IActionManageService;
import com.eelpo.common.util.BlankUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * 
 * 动作管理服务具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Service
public class ActionManageService implements IActionManageService {

	@Resource
	protected IActionManageDao actionManageDao;

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#getActionById(java.
	 *      lang.String)
	 */
	@Override
	public Action getActionById(String id) throws ServiceException {
		TAction taction = actionManageDao.getActionById(id);

		if (BlankUtils.isBlank(taction)) {
			throw new ServiceException("查询的动作不存在！");
		}

		return po2vo(taction);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#checkExist(java.lang
	 *      .String)
	 */
	@Override
	public boolean checkExist(String code) throws ServiceException {
		return actionManageDao.checkExist(code);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#addAction(com.eelpo
	 *      .cmmp.uac.dao.vo.Action)
	 */
	@Override
	public String addAction(Action action) throws ServiceException {

		// 检查动作是否已经存在
		if (actionManageDao.checkExist(action.getCode())) {
			throw new ServiceException("动作代码[" + action.getCode() + "]已经存在！");
		}

		return actionManageDao.addAction(vo2po(action));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#updateAction(com.eelpo
	 *      .cmmp.uac.dao.vo.Action)
	 */
	@Override
	public void updateAction(Action action) throws ServiceException {

		// 检查实体类型是否已经存在
		if (actionManageDao.checkExistByUpdate(action.getId(), action.getCode())) {
			throw new ServiceException("动作代码[" + action.getCode() + "]已经存在！");
		}

		actionManageDao.updateAction(vo2po(action));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#disabledAction(com.eelpo.cmmp.uac.dao.vo.Action)
	 */
	@Override
	public void disabledAction(Action action) throws ServiceException {

		TAction taction = actionManageDao.getActionById(action.getId());

		taction.setStatus(Status.DISABLED.getValue());

		actionManageDao.updateAction(taction);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#enabledAction(com.eelpo.cmmp.uac.dao.vo.Action)
	 */
	@Override
	public void enabledAction(Action action) throws ServiceException {

		TAction taction = actionManageDao.getActionById(action.getId());

		taction.setStatus(Status.ENABLED.getValue());

		actionManageDao.updateAction(taction);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#deleteAction(com.eelpo
	 *      .cmmp.uac.dao.vo.Action)
	 */
	@Override
	public void deleteAction(Action action) throws ServiceException {

		if (actionManageDao.checkActionUsed(action.getId())) {
			throw new ServiceException("无法删除，该动作已经被使用！");
		}

		actionManageDao.deleteAction(action.getId());
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#getEnabledActions()
	 */
	@Override
	public List<Action> getEnabledActions() throws ServiceException {
		return po2vo(actionManageDao.getEnabledActions());
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#getActionsPaing(com
	 *      .eelpo.cmmp.uac.dao.vo.Action, int, int)
	 */
	@Override
	public List<Action> getActionsPaing(Action action, int first, int max) throws ServiceException {
		return po2vo(actionManageDao.getActionsPaing(action, first, max));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IActionManageService#getActionsCount(com
	 *      .eelpo.cmmp.uac.dao.vo.Action)
	 */
	@Override
	public int getActionsCount(Action action) throws ServiceException {
		return actionManageDao.getActionsCount(action);
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param action
	 *            动作值对象
	 * @return 返回动作实体对象
	 */
	public static TAction vo2po(Action action) {

		TAction taction = new TAction();
		BeanUtils.copyProperties(action, taction, new String[] { "status" });

		taction.setStatus(Status.valueOf(action.getStatus()));

		return taction;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param taction
	 *            动作实体对象
	 * @return 返回动作值对象
	 */
	public static Action po2vo(TAction taction) {
		Action action = new Action();
		BeanUtils.copyProperties(taction, action, new String[] { "status" });
		action.setStatus(Status.valueOf(taction.getStatus()));
		return action;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param tactions
	 *            动作实体对象列表
	 * @return 返回动作值对象列表
	 */
	public static List<Action> po2vo(List<TAction> tactions) {
		List<Action> actions = new ArrayList<Action>();
		for (TAction taction : tactions) {
			actions.add(po2vo(taction));
		}
		return actions;
	}

}
